Skip to content

Method: static {...}

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.owl2java;
19:
20: import cz.cvut.kbss.jopa.owl2java.prefix.PrefixMap;
21: import org.semanticweb.owlapi.model.IRI;
22: import org.semanticweb.owlapi.model.OWLOntologyID;
23:
24: import java.text.Normalizer;
25: import java.util.Arrays;
26: import java.util.Objects;
27: import java.util.Optional;
28:
29: /**
30: * Generates Java names based on IRI identifiers.
31: */
32: public class JavaNameGenerator {
33:
34: private static final String[] JAVA_KEYWORDS = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"};
35:
36: private static final char SEPARATOR = '_';
37:
38: private final PrefixMap prefixMap;
39:
40: public JavaNameGenerator(PrefixMap prefixMap) {this.prefixMap = prefixMap;}
41:
42: /**
43: * Returns a valid Java identifier extracted from the specified IRI.
44: * <p>
45: * If the IRI contains a non-empty fragment, it is used. Otherwise, the part after the last slash is used as the
46: * name.
47: *
48: * @param iri IRI to extract name from
49: * @return Java name based on the specified IRI
50: */
51: public String generateJavaNameForIri(IRI iri) {
52: if (iri.getFragment() != null && !iri.getFragment().isEmpty()) {
53: return makeNameValidJava(iri.getFragment());
54: } else {
55: String strIri = iri.toString();
56: if (strIri.charAt(strIri.length() - 1) == '/') {
57: strIri = strIri.substring(0, strIri.length() - 1);
58: }
59: int x = strIri.lastIndexOf("/");
60: return makeNameValidJava(strIri.substring(x + 1));
61: }
62: }
63:
64: /**
65: * Returns a valid Java identifier extracted from the specified IRI, prefixed with prefix registered for the
66: * specified ontology IRI (if available).
67: * <p>
68: * If the IRI contains a non-empty fragment, it is used. Otherwise, the part after the last slash is used as the
69: * name.
70: * <p>
71: * If the ontology is anonymous, no prefix is added to the extracted name. If no prefix is registered for the
72: * ontology, a prefix represented by extracting a java name from the ontology IRI is used.
73: *
74: * @param iri IRI to extract name from
75: * @param ontologyId Ontology identifier
76: * @return Java name based on the specified IRI
77: */
78: public String generatePrefixedJavaNameForIri(IRI iri, OWLOntologyID ontologyId) {
79: if (ontologyId.isAnonymous()) {
80: return generateJavaNameForIri(iri);
81: }
82: assert ontologyId.getOntologyIRI().isPresent();
83: final IRI ontologyIri = ontologyId.getOntologyIRI().get();
84: return makeNameValidJava(prefixMap.getPrefix(ontologyIri)
85: .orElse(generateJavaNameForIri(ontologyIri))) + SEPARATOR + generateJavaNameForIri(iri);
86: }
87:
88: /**
89: * Gets the prefix registered for an ontology with the specified identifier.
90: *
91: * @param ontologyIri Ontology IRI
92: * @return Optional ontology prefix
93: */
94: public Optional<String> getOntologyPrefix(IRI ontologyIri) {
95: Objects.requireNonNull(ontologyIri);
96: return prefixMap.getPrefix(ontologyIri);
97: }
98:
99: /**
100: * Checks whether a prefix exists for the specified ontology identifier.
101: *
102: * @param ontologyIri Ontology IRI
103: * @return {@code true} if a prefix has been resolved for ontology IRI, {@code false} otherwise
104: */
105: public boolean hasPrefix(IRI ontologyIri) {
106: return prefixMap.hasPrefix(ontologyIri);
107: }
108:
109: /**
110: * Returns the specified name sanitized for Java.
111: * <p>
112: * This means the result of this function can be used as/in a Java variable/field/class name.
113: *
114: * @param name The name to sanitize
115: * @return Valid Java identifier
116: */
117: public static String makeNameValidJava(String name) {
118: String res = name.trim().replace('-', SEPARATOR).replace("'", "_quote_")
119: .replace(".", "_dot_").replace(',', '_')
120: .replace("#", "");
121: // Replace non-ASCII characters with ASCII ones
122: res = Normalizer.normalize(res, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
123: if (Arrays.binarySearch(JAVA_KEYWORDS, res) >= 0) {
124: res = SEPARATOR + res;
125: }
126: return res;
127: }
128:
129: /**
130: * Converts the specified name to the Java camel case notation.
131: * <p>
132: * This process removes underscores used to generate the name.
133: *
134: * @param name Generated name
135: * @return Converted camel case name
136: */
137: public static String toCamelCaseNotation(String name) {
138: StringBuilder result = new StringBuilder();
139: for (String w : name.split(Character.toString(SEPARATOR))) {
140: if (!w.isEmpty()) {
141: result.append(w.substring(0, 1).toUpperCase()).append(w.substring(1));
142: }
143: }
144: return result.toString();
145: }
146: }